package cn.jy.boot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MYAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //自定义成功之后返回的信息
        Map<String,Object> returnInfo = new HashMap<>();
        returnInfo.put("msg","登陆成功");
        returnInfo.put("code","200");
        returnInfo.put("authentication",authentication);
//        把要返回的信息转化为json字符串
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(returnInfo);
//        把json字符串返回
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().print(json);
    }
}
